package com.lsz.word.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 存放单词的字典对象
 * @author lsz
 *
 */
public class Directory extends HashMap<String, Integer>{
	private static Directory directory = null;
	private Directory(){super();};
	
	/**
	 * 实例化对象
	 * @param wordList
	 * @return
	 */
	public static Directory getInstance(List<String> wordList){
		if(directory == null){
			install(wordList);
			return directory;
		}else{
			return directory;
		}
	}
	
	/**
	 * 加载字典
	 */
	private static void install(List<String> wordList){
		directory = new Directory();
		List<String> list = Words.allToLow(wordList);
		for(String str :list){
			if(directory.get(str)==null){
				directory.put(str, 1);
			}else{
				int i = directory.get(str);
				directory.put(str, i+1);
			}
		}
	}
	
	/**
	 * 得到该单词出现的额次数
	 * @param word
	 */
	public int wordTimes(String word){
		Integer times = directory.get(word);
		if(times == null){
			return 0;
			
		}else{
			return times;
			
		}
		
	}
	
}
